package com.lin.eat.serivce.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lin.eat.dao.GoodsCommentMapper;
import com.lin.eat.dao.GoodsMapper;
import com.lin.eat.entry.*;
import com.lin.eat.serivce.GoodsService;
import com.lin.eat.util.FileUtil;
import com.lin.eat.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsCommentMapper goodsCommentMapper;

    /**
     * 获取推荐商品列表
     * @return
     */
    @Override
    public ServerResponse<List<Goods>> recommendGoods() {
        List<Goods> goodsList = goodsMapper.recommendGoods();
        ServerResponse<List<Goods>> serverResponse = null;
        serverResponse = goodsList.size()>0 ? ServerResponse.createBySuccess(goodsList) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 获取热销商品列表
     * @return
     */
    @Override
    public ServerResponse<Map<String, List<Goods>>> hotGoods() {
        List<Goods> goodsRank = goodsMapper.hotGoodsRank();
        List<Goods> goodsSwiper = goodsMapper.hotGoodsSwiper();
        List<Goods> goodsDiscounts = goodsMapper.hotGoodsDiscounts();
        HashMap<String,List<Goods>> map = new HashMap<>();
        map.put("rank",goodsRank);
        map.put("swiper",goodsSwiper);
        map.put("discounts",goodsDiscounts);
        ServerResponse<Map<String, List<Goods>>> serverResponse = null;
        if( goodsRank == null || goodsSwiper == null || goodsDiscounts == null){
            serverResponse = ServerResponse.createByError(null);
        } else {
            serverResponse = ServerResponse.createBySuccess(map);
        }
        return serverResponse;
    }

    /**
     * 获取全部商品
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public ServerResponse<PageInfo<Goods>> selectAllGoods(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Goods> goodsList = goodsMapper.selectAllGoods();
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        ServerResponse<PageInfo<Goods>> serverResponse = null;
        serverResponse = goodsList.size()>0 ? ServerResponse.createBySuccess(pageInfo) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 获取商品详情
     * @param g_id
     * @return
     */
    @Override
    public ServerResponse<Map<String,GoodsDetail>> getGoodsDetail(String g_id) {
        GoodsDetail goodsDetail = goodsMapper.findGoodDetailByGid(g_id);
        List<GoodsComment> goodsCommentList = goodsCommentMapper.selectCommentByGid(g_id);
        goodsDetail.setCommentList(goodsCommentList);
        ServerResponse<Map<String,GoodsDetail>> serverResponse = null;
        HashMap<String,GoodsDetail> map = new HashMap<>();
        map.put("detail",goodsDetail);
        serverResponse = goodsDetail != null ? ServerResponse.createBySuccess(map) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 通过关键字查找商品
     * @param pageNum
     * @param pageSize
     * @param key
     * @return
     */
    @Override
    public ServerResponse<PageInfo<Goods>> getGoodsByKey(Integer pageNum, Integer pageSize, String key) {
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goodsList = goodsMapper.getGoodsByKey(key);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        ServerResponse<PageInfo<Goods>> serverResponse = null;
        serverResponse = goodsList.size()>0 ? ServerResponse.createBySuccess(pageInfo) : ServerResponse.createByError(null);
        return serverResponse;
    }

    @Override
    public void addGoods(MultipartFile file, Good good) throws IOException {
        String imageUrl = FileUtil.uploadGood(file);
        Goods goods = new Goods(UuidUtil.createUUID(),"19a4a29af2444244b4255d9b40337e79",good.getG_name(),imageUrl,good.getG_synopsis(),good.getG_price());
        goodsMapper.addGoods(goods);
    }

}
